package com.chen.springboot.controller;

import com.chen.springboot.common.AuthAccess;
import com.chen.springboot.common.Result;
import com.chen.springboot.service.AiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AI分析控制器
 */
@RestController
@RequestMapping("/ai")
public class AiController {

    @Autowired
    private AiService aiService;

    /**
     * 商品对比分析接口
     */
    @AuthAccess
    @PostMapping("/compare")
    public Result<Map<String, String>> compareProducts(@RequestBody Map<String, Object> request) {
        try {
            @SuppressWarnings("unchecked")
            List<Map<String, Object>> products = (List<Map<String, Object>>) request.get("products");
            
            if (products == null || products.size() < 2) {
                return Result.error("400", "至少需要2个商品进行对比");
            }

            // 调用AI服务进行分析
            String analysis = aiService.compareProducts(products);
            
            Map<String, String> resultData = new HashMap<>();
            resultData.put("analysis", analysis);
            
            return Result.success(resultData);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("500", "AI分析失败：" + e.getMessage());
        }
    }
}

